package com.yksj.healthtalk.ui.friend;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.SystemUtils;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 修改好友备注
 * @author Administrator
 *
 */
public class FriendRemark extends BaseFragmentActivity implements OnClickListener {
	String userid;//别人的id
	EditText mEditText;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.friend_remark);
		initTitle();
		initView();
	}

	private void initView() {
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("修改备注名");
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText("确定");
		userid=getIntent().getStringExtra("id");
		final TextView mView= (TextView) findViewById(R.id.count);
		mEditText= (EditText) findViewById(R.id.name);
		
		if(getIntent().hasExtra("remark")){
			mEditText.setText(getIntent().getStringExtra("remark"));
		}
		 InputMethodManager imm = (InputMethodManager)getSystemService(this.INPUT_METHOD_SERVICE);   
		 imm.showSoftInput(mEditText, 0);
		mEditText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(s.length()>10){
					ToastUtil.showShort(FriendRemark.this, "备注字数已经超过8个字!!!");
				}else{
					mView.setText(s.length()+"/8");
				}
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			@Override
			public void afterTextChanged(Editable s) {
			}
		});
	}

	@Override
	public void onClick(View v) {
		SystemUtils.hideSoftBord(this, mEditText);
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_right2:
			String remark=mEditText.getText().toString();
			submit(remark);
			if(remark!=null&&(!"".equals(remark))){
				
			}
			break;
		}
		
	}
	
	private void submit(final String name){
		HttpRestClient.doHttpRemarkName(name,SmartFoxClient.getLoginUserId(),userid,new AsyncHttpResponseHandler(this){
			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				try {
					JSONObject object=new JSONObject(content);
					if(object.has("error_message")){
						ToastUtil.showBasicShortToast(getApplicationContext(), object.getString("error_message"));
					}else{
						ToastUtil.showToastPanl("修改成功");
						Intent intent = getIntent();
						intent.putExtra("remark",name );
						setResult(Activity.RESULT_OK, intent);
						finish();
						overridePendingTransition(R.anim.back_enter, R.anim.back_exit);	
					}
				} catch (Exception e) {
				}
			}
		});
	}
	
	
}
